package com.dong.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author dongjian
 * <p>
 * 发布确认 （高级）配置文件
 *
 * 备份交换机的优先级比消息回退要高，如果消息不可达首先选择转发到备份交换机
 * 如果备份交换机也不可达就会回退
 * </p>
 */
@Configuration
public class ConfirmConfig {

    /**
     * 交换机名称
     */
    public static final String CONFIRM_EXCHANGE_NAME = "confirm.exchange";
    /**
     * 队列名称
     */
    public static final String CONFIRM_QUEUE_NAME = "confirm.queue";
    /**
     * 路由键
     */
    public static final String CONFIRM_ROUTING_KEY = "key1";
    /**
     * 备份交换机名称
     */
    public static final String BACKUP_EXCHANGE_NAME = "backup.exchange";
    /**
     * 备份队列名称
     */
    public static final String BACKUP_QUEUE_NAME = "backup.queue";
    /**
     * 警告队列名称
     */
    public static final String WARNING_QUEUE_NAME = "warning.queue";

    /**
     * 声明交换机
     *
     * @return
     */
    /*@Bean("confirmExchange")
    public DirectExchange confirmExchange() {
        return new DirectExchange(CONFIRM_EXCHANGE_NAME);
    }*/
    @Bean("confirmExchange")
    public DirectExchange confirmExchange() {
        return ExchangeBuilder.directExchange(CONFIRM_EXCHANGE_NAME)
                .durable(true)
                // 转发到备份交换机上
                .withArgument("alternate-exchange", BACKUP_EXCHANGE_NAME)
                .build();
    }

    /**
     * 声明备份交换机
     *
     * @return
     */
    @Bean("backUpExchange")
    public FanoutExchange backUpExchange() {
        return new FanoutExchange(BACKUP_EXCHANGE_NAME);
    }

    /**
     * 队列声明
     *
     * @return
     */
    @Bean("confirmQueue")
    public Queue confirmQueue() {
        return QueueBuilder.durable(CONFIRM_QUEUE_NAME).build();
    }

    /**
     * 备份队列
     *
     * @return
     */
    @Bean("backUpQueue")
    public Queue backUpQueue() {
        return QueueBuilder.durable(BACKUP_QUEUE_NAME).build();
    }

    /**
     * 警告队列
     *
     * @return
     */
    @Bean("warningQueue")
    public Queue warningQueue() {
        return QueueBuilder.durable(WARNING_QUEUE_NAME).build();
    }
    // ------------------------绑定------------------------------

    /**
     * 交换机绑定队列绑定
     *
     * @param confirmExchange 交换机
     * @param confirmQueue    队列
     * @return
     */
    @Bean
    public Binding confirmQueueBindConfirmExchange(@Qualifier("confirmExchange") DirectExchange confirmExchange,
                                                   @Qualifier("confirmQueue") Queue confirmQueue) {
        return BindingBuilder.bind(confirmQueue).to(confirmExchange).with(CONFIRM_ROUTING_KEY);
    }

    /**
     * 备份交换机绑定备份队列
     *
     * @param backUpExchange 备份交换机
     * @param backUpQueue    备份队列
     * @return
     */
    @Bean
    public Binding backUpQueueBindBackUpExchange(@Qualifier("backUpExchange") FanoutExchange backUpExchange,
                                                 @Qualifier("backUpQueue") Queue backUpQueue) {
        return BindingBuilder.bind(backUpQueue).to(backUpExchange);
    }

    /**
     * 备份交换机绑定警告队列
     *
     * @param backUpExchange 备份交换机
     * @param warningQueue   警告队列
     * @return
     */
    @Bean
    public Binding warningQueueBindBackUpExchange(@Qualifier("backUpExchange") FanoutExchange backUpExchange,
                                                  @Qualifier("warningQueue") Queue warningQueue) {
        return BindingBuilder.bind(warningQueue).to(backUpExchange);
    }
}
